//
//  GetCouponsListVm.swift
//  Pods
//
//  Created by eme on 2017/3/29.
//
//
/*
 *获取优惠券列表
 */
import SwiftyJSON
import RxSwift
import Result
import ModelProtocol
import UtilCore
import NetWorkCore

class GetCouponsListVm:Base_Vm{
    
    let getCouponsList = PublishSubject<String>()
    let backDate = PublishSubject<[VouchersModel]>()
    let refreshing = Variable(false)
    var loading : Observable<Bool>
    
     override init() {
        
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        super.init()
        
        
        
        let request = Observable.of(
            getCouponsList.asObservable(),
            reloadTrigger.withLatestFrom(getCouponsList.asObserver()))
            .merge()
            .map{type in
                
                return OrderFormApi.getCouponsList(vstate: type)
                
            }.shareReplay(1)
        
        let response = request.emeRequestApiForArray(VouchersModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        
        response.subscribe(onNext:{[unowned self] result in
            
            switch result{
            case .success(let data):
                self.backDate.onNext(data)
            case .failure(let error):
                self.refresherror.onNext(error)
            }
            
        }).addDisposableTo(disposeBag)
        
    }
    
}
